#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

typedef struct node {
    int data;
    struct node *left, *right;
} Node;

Node *createNode(int data) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}

Node *insertNode(Node *node, int data) {
    if (node == NULL) return createNode(data);

    if (data < node->data)
        node->left  = insertNode(node->left, data);
    else if (data > node->data)
        node->right = insertNode(node->right, data);  

    return node;
}

void searchNode(Node *root, int key, char *path, int *depth) {
    if (root == NULL) return;

    char buffer[11];
    sprintf(buffer, "%d->", root->data); 
    strncat(path, buffer, sizeof(buffer));
    *depth += 1;

    if (root->data == key) {
        path[strlen(path)-2] = '\0';  
        return;
    }

    if (root->data > key) 
        searchNode(root->left, key, path, depth);
    else 
        searchNode(root->right, key, path, depth);
}

int main() {
    int t, key, data;
    scanf("%d", &t);

    while (t--) {
        Node *root = NULL; 
        while (scanf("%d", &data), data != -1)
            root = insertNode(root, data);
            
        char path[100] = "";
        int depth = 0;

        scanf("%d", &key);
        searchNode(root, key, path, &depth);
        printf("%s\n", path);
        printf("%d\n", depth);

        free(root);  
    }
    return 0;
}
